/**
*  \file global.h
*
*  \brief
*
*  \authors Dajin Li  dajin.li@linde-.de
*
*
*
*/

#ifndef  GLOBAL_H
#define GLOBAL_H

/*================================================[ public includes  ]================================================*/
#include "stm32f10x.h"
#include "cmsis_os.h"
/*================================================[ public defines   ]================================================*/
#define TASK_MAX	                            13	                    //Number of tasks,not inclueding watchdog task //used buy watchdog task
#define MAXJ1939DATA 	                        9

#define ApplicationSwAddress                    0x8008000
#define ApplicationEndAddress                   0x8040000
#define BootBISAddress                          0x8000188
#define AppBISAddress                           0x8008188

//all the "active" screens
#define SCREEN_NONE				                0x0000
#define SCREEN_LOGO                             0x0001
#define SCREEN_INFO                             0x0002
#define SCREEN_PIN                              0x0004
#define SCREEN_FACTORY                          0x0008
#define SCREEN_OSHA                             0x0010
#define SCREEN_HOME                             0x0020
#define SCREEN_MENU				                0x0040
#define SCREEN_FAULT				            0x0080


#define SCREEN_INIT					            0x8000

#define BIT0 		                            0x01
#define BIT1		                            0x02
#define BIT2 		                            0x04
#define BIT3 		                            0x08
#define BIT4 		                            0x10
#define BIT5 		                            0x20
#define BIT6 		                            0x40
#define BIT7 		                            0x80
#define BIT8 		                            0x0100	 
#define BIT9 		                            0x0200	
#define BIT10 	                                0x0400	
#define BIT11 	                                0x0800	
#define BIT12	                                0x1000	
#define BIT13 	                                0x2000
#define BIT14 	                                0x4000	
#define BIT15 	                                0x8000
#define BIT16 	                                0x10000
#define BIT17 	                                0x20000
#define BIT18 	                                0x40000
#define BIT19 	                                0x80000
#define BIT20 	                                0x100000
#define BIT21 	                                0x200000
#define BIT22 	                                0x400000
#define BIT23 	                                0x800000
#define BIT24 	                                0x1000000
#define BIT25 	                                0x2000000
#define BIT26 	                                0x4000000
#define BIT27 	                                0x8000000
#define BIT28 	                                0x10000000
#define BIT29 	                                0x20000000
#define BIT30 	                                0x40000000
#define BIT31 	                                0x80000000

#define ENTER_KEY_GPIO_GROUP	                GPIOE
#define UP_KEY_GPIO_GROUP		                GPIOE
#define DOWN_KEY_GPIO_GROUP   	                GPIOE
#define BACK_KEY_GPIO_GROUP	                    GPIOE

#define	ENTER_KEY_PIN				            GPIO_Pin_7	//PE.07
#define	UP_KEY_PIN					            GPIO_Pin_10	//PE.10	
#define	DOWN_KEY_PIN				            GPIO_Pin_8	//PE.8
#define	BACK_KEY_PIN				            GPIO_Pin_9	//PE.9

#define	BUZZER_GPIO_GROUP	                    GPIOA
#define BUZZER_GPIO_PIN		                    GPIO_Pin_12

                                                
// digital input ports detecting the LED Fault codes

#define UPDT_DISPLAY_ID				            0
#define UPDT_LEDICONS_ID 				        1
#define UPDT_OUTPUTS_ID				            2
#define UPDT_BDI_ID						        3
#define UPDT_TOD_ID						        4
#define UPDT_PWM_ID 					        5
#define CAN_ID 							        6
#define J1939_ID 							    7
#define SAMPLE_SW_INPUTS_ID		                8
#define SAMPLE_ANALOG_INPUTS_ID	                9
#define SAMPLE_PB_ID 				            10
#define UPDT_HRMTRS_ID 			                11
#define SPI_EEPROM_ID			                12


#define OFF_CHIP_RTC                              

// defines for task health
#define ALIVE	                               0		
#define UNKNOWN                                0x5A


#define TIMER_TICK                             1

#define UPDT_DISPLAY_ITV 			           (100 / TIMER_TICK)	    // 100ms
#define UPDT_LEDICONS_ITV 			           (50 / TIMER_TICK)        // 50ms
#define UPDT_OUTPUTS_ITV 			           (50 / TIMER_TICK)	    // 50ms
#define UPDT_BDI_ITV 				           (500 / TIMER_TICK)	    // 1000ms
#define UPDT_BDIMEAS_ITV                       (5 / TIMER_TICK)	        // 10ms
#define UPDT_TOD_ITV 				           (500 / TIMER_TICK)	    // 500ms
                                               
#define UPDT_PWM_ITV 				           (2 / TIMER_TICK)	        // 4ms
#define CAN_ITV 					           (2 / TIMER_TICK)         // 4ms
#define SAMPLE_SW_INPUTS_ITV 		           (2 / TIMER_TICK)		    // 2ms  
#define SAMPLE_ANALOG_INPUTS_ITV	           (10 / TIMER_TICK)		// 10ms  
#define FAULT_INPUTS_ITV 			           (20 / TIMER_TICK)		// 20ms
                                               
#define SAMPLE_PB_ITV				           (2 / TIMER_TICK)		    // 2ms
#define UPDT_HRMTRS_ITV 			           (500 / TIMER_TICK)	    // 500ms
#define SPI_EEPROM_ITV			               (20 / TIMER_TICK)		// 20ms
#define RESET_IWDOG_ITV 			           (1800 / TIMER_TICK)	    // 1800ms
#define MAX_NUM_UNITS                          14


#define MAX_ACTIVE_FAULT                       16
#define MAX_STORED_FAULT                       16
#define MAX_DISPLAY_FAULT                      16
#define MAX_ENG_ACTIVE_FAULT                   9
#define MAX_ACTIVE_FAULT4                      4

#define VAR_LENGTH_MASK                        0x00FF
#define READ_WRITE		                       0x8000
#define LIMIT_CHK		                       0x4000
#define SIGNED_VAR		                       0x2000
#define STR_VAR		                           0x1000
#define SAVE_TO_EE		                       0x0800
#define VAR_CLR_SCREEN                         0x0400
#define VAR_USED                               0x0200
#define KWP_MULTI_MSG                          0x0100

#define	ENTER_KEY_PRESSED			           BIT0
#define	LONG_ENTER_KEY_PRESSED	               BIT1

#define	UP_KEY_PRESSED			               BIT2
#define	DOWN_KEY_PRESSED			           BIT3
#define	BACK_KEY_PRESSED			           BIT4
#define	UP_DOWN_KEY_PRESSED		               BIT5

#define KWP_RD_COMM_MM                         0x40
#define KWP_RD_ECU_MM                          0x20
#define KWP_RD_DATA_MM                         0x10
#define KWP_SECURITY_MM                        0x08
#define KWP_ECU_RESET_MM                       0x04
#define KWP_START_ROUT_MM                      0x02
#define KWP_START_FLASH_MM                     0x01
#define KWP_NONE_MM                            0x00

#define INTER_TRUCK_TYPE                       T1275LI//T1276//T1275LI//T1347//T1275//T1276LI
//#define USING_BR1347_OBJ
#define USING_BR1275_76_OBJ
//#define USING_NEW_LOGO
//#define RELEASE 
//#define USING_OLD_LOGO

#define USING_LION_LOGO

//LCD Back light maxiam and miniamm limit
#define MAX_LCD_BKLEVEL                        6
#define MIN_LCD_BKLEVEL                        0

//LCD Back light status, on or off
#define BK_LIGHT_ON			0x01
#define BK_LIGHT_OFF		0x00

#define countof(a)                             (sizeof(a) / sizeof(*(a)))


//#define S1D19600 
#define ST7511             
                                               
#define MAX(val, val2)                         ((val) < (val2) ? (val2) : (val))
#define MIN(val, val2)                         ((val) < (val2) ? (val) : (val2))
#define ABS(val)                               ((val) < 0 ? -(val) : (val))
/*================================================[ public datatypes ]================================================*/

typedef char               S8;
typedef unsigned char      U8;
typedef short              S16;
typedef unsigned short     U16;
typedef int                S32;
typedef unsigned int       U32;
typedef long long          S64;
typedef unsigned long long U64;

/* Types */
typedef int32_t            s32;
typedef int16_t            s16;
typedef int8_t             s8;

typedef uint64_t           u64;                     
typedef uint32_t           u32;
typedef uint16_t           u16;
typedef uint8_t            u8;

typedef __IO uint32_t      vu32;
typedef __IO uint16_t      vu16;
typedef __IO uint8_t       vu8;
typedef enum { FALSE = 0, TRUE  = !FALSE } bool;       

typedef U8 MemCanObjData;
//pin code structure
typedef struct
{
	U8 PinCodeNum[4];
	U8 PinCode_Pt;
} PinCodeType;

//Display Mode
typedef enum
{
    MODE_Standby,
	MODE_PinCode,
	MODE_Welcome,
	MODE_ProInfo,
    MODE_ErrCode,
    MODE_ErrWarning,
	MODE_InitErrCode,
    MODE_OilErrCode,
    MODE_TempErrCode,
    MODE_TempErrCode_Next,
	MODE_ShowTime,
	MODE_ShowHourmeter,
	MODE_ShowService,
	MODE_UserErrCode,
	MODE_AdjustLightness,
    MODE_DeleteErrCode_Diag,
    MODE_MaintErrCode,
    Mode_BatCutoff,
    Mode_BatLow,
    MODE_Admin,
    MODE_ResetPincode,
    MODE_DigiCodeEnsure,
}Screen_Mode_type;


enum can_baud_rates  
{
    CAN_100KBAUD = -1,  // -1
    CAN_125KBAUD,       //  0
    CAN_250KBAUD,       //  1
    CAN_500KBAUD,       //  2
    CAN_BAD_BAUD 
};
//Small Icon Struct
typedef struct
{
	void *const lang_str;	//location y row
	s16 length;			    //location x column
}LangStr;

//chinese char 
typedef struct
{
	u8 special_char;
    u8 width;
 	u8 *SFrenchData;		//Icon data
}LangChar;

typedef struct stored_fault_st
{
    u8 month;
    u8 day;
    u8 hour;
    u8 minutes;
    u8 sec;
    u8 year;
    u8 save_flag;
    u16 code;
    u32 hourmeter;
}E_STORED_FAULT_PARAM;

typedef void (*KWPGatewayEvent)( void *, void *, u16 attr, u32 min_value, u32 max_value);

typedef enum
{
  KWP_GATEWAY_SDO_RD = 0,
  KWP_GATEWAY_SDO_WR = 1
  
}KWPGatewaySDOType;

typedef struct
{
  u8                  kwpMMServType;
  u32                 ObjectDictId;
  KWPGatewaySDOType   SDOType;
  KWPGatewayEvent     event;

}KWPGatewayMultiMsgTbl;

typedef struct
{
  bool                    mmBusy;
  u16                     kwpId;     // KWP object id
  u8                      kwpServId; // service id
  u8                      srcKwpAddr; // KWP source id
  u8                      destNodeId;
  u8                      step;
  u8                      scale;
  u16                     min;
  u16                     max;
  u16                     attr;
  u8                      startRoutEntryOpt;
  KWPGatewayMultiMsgTbl   *tbl;
  void                    *data;

} KWPGatewayMultiMsgServ;

//kwp data params
typedef struct
{
  void *var;              //location y row
  u16 attr;                       //location x column
  u16 ee_addr;
  u16 kwp_id;
  s32 min;                //Icon flash or not
  s32 max;            //Icon data
  s32 default_value;
} kwp_data_st;

//kwp data params
typedef struct
{
  void *var;        //location y row
  u8 scale;
  u16 attr;               //location x column
  u16 kwp_id;             //
  u32 can_obj_id;         //
  KWPGatewayMultiMsgTbl * tbl;
  KWPGatewayEvent         event;
  s32 min;                //Icon flash or not
  s32 max;                //Icon data
} kwp_gateway_data_st;


typedef struct 
{ 
    s32 fault_id;
    u16 pcode;
    u8* en_msg;
    u8 *cn_msg;
	u8 *french_msg;
	u8 *spanish_msg;
}J1939_fault_msg_st;

typedef struct 
{ 
    u16 fault_code;
    u16 delay;
    u8 fault_index;
    u8 reset;
}Truck_active_fault_st;

typedef struct 
{ 
    s32 fault_id;
    u8* en_msg;
    u8 *cn_msg;
	u8 *french_msg;
	u8 *spanish_msg;
}Truck_fault_msg_st;


typedef struct 
{ 
    u8* en_menu;
    u8 *cn_menu;
	u8 *french_menu;
	u8 *spanish_menu;
}Menu_language_st;

typedef struct 
{ 
    u8 fault_id;
    u8* en_msg;
    LangStr *cn_msg;
	u8 filter_mask;
}Controller_fault_msg_st;


typedef struct 
{ 
    u32 object_id;
    u16 attr;
    u32 value;
    u32 min;
    u32 max;
    u32 def;
    u8  units;
    u8 scale;
}CAN_param_st;

typedef struct 
{ 
    u16 kwp_index;    // kwp index
    u32 object_id;    // can object id
    u16 can_id;       // can node id
}CAN_KWP_param_st;



typedef struct J1939_FAULT_CODE
{   
    u32 spn;
    u32 object_id;
    u8 fmi;		
    u8 oc_count;
    u8 spn_cm;
}J1939_fault_code_st;

typedef struct 
{
    u8 fmi;		
    u8 month;
    u8 day;
    u8 hour;
    u8 minutes;
    u8 sec;
    u8 year;
    u8 save_flag;
    u32 spn;
    u32 hourmeter;
}J1939_STORED_FAULT_PARAM;


//ascii ICon Struct
typedef struct
{
    u16 code;               // unicode
    u8 width;
 	u8 *AsciiIconData;		//Icon data
}UniCharIcon;



/* Truck type, if truck type is same, then the menu is same, like BR1219 and BR1319, the menu is same*/
enum
{ 
    BR1219          = 0,               // 0
    BR1319          = 0,               // 0
    BR1285          = 1,               // 1
    BR1286          = 1,               // 1
    BR1346          = 2,               // 2
    BR1347          = 3,               // 3
    BR1275          = 3,               // 3
    BR1275LI        = 3,               // 3
    BR1276          = 3,               // 3
    BR1410          = 4,               // 4 
    BR1411          = 4,               // 4
    BR5195          = 5,               // 5
    BR5215          = 6,               // 6
    BR1220          = 7,               // 7
    BR352           = 7,               // 7 
    BR1219_CHN      = 8,               // 8 
    BR1218          = 8,               // 8
    BR1216          = 8,               // 8
    BR115           = 9,               // 9
    BR116           = 9,               // 9
    BR1123          = 9,               // 9
    BR1276LI        = 3,               // 3 
};

/* Truck id, each truck has different truck id*/
enum
{
    BR1219_ID         = 300100,       // 0
    BR1319_ID         = 300200,       // 1
    BR1285_ID         = 300300,       // 2
    BR1286_ID         = 300400,       // 3
    BR1346_ID         = 300500,       // 4
    BR1347_ID         = 300600,       // 5
    BR1410_ID         = 300700,       // 6 
    BR1411_ID         = 300800,       // 7
    BR5195_ID         = 300900,       // 8
    BR5215_ID         = 301000,       // 9
    BR1275_ID         = 301100,       // 10
    BR1275LI_ID       = 301200,       // 11
    BR1276_ID         = 301300,       // 12
    BR1220_ID         = 301400,       // 13
    BR352_ID          = 301500,       // 14 
    BR1219_CHN_ID     = 301600,       // 15
    BR1218_ID         = 301700,       // 16
    BR1216_ID         = 301800,       // 17
    BR115_ID          = 301900,       // 18    
    BR116_ID          = 302000,       // 19   
    BR1123_ID         = 302100,       // 20
    BR1276LI_ID       = 302200,       // 21 
};




/* complier option*/
enum 
{ 
    T1219         = 0,                   // 0
    T1319         = 1,                   // 1
    T1285         = 2,                   // 2
    T1286         = 3,                   // 3
    T1346         = 4,                   // 4
    T1347         = 5,                   // 5
    T1410         = 6,                   // 6 
    T1411         = 7,                   // 7
    T5195         = 8,                   // 8
    T5215         = 9,                   // 9
    T1275         = 10,                  // 10
    T1275LI       = 11,                  // 11
    T1276         = 12,                  // 12
    T1220         = 13,                  // 13
    T352          = 14,                  // 14
    T1219_CHN     = 15,                  // 15
    T1218         = 16,                  // 16
    T1216         = 17,                  // 17
    T115          = 18,                  // 18   
    T116          = 19,                  // 19 
    T1123         = 20,                  // 20 
    T1276LI       = 21,                  // 21 
};

/*================================================[ public variables ]================================================*/
extern u8 key_status;
extern Screen_Mode_type Screen_Mode;
extern PinCodeType Pin_Code;;
extern u8 task_status[];		// Flags to indicate health (status) of each task
extern u8 TechPwd[];		//password save in EEPROM
extern u8 ServicePwd[];
extern u8 in_menu;
extern u8 LanguageSelect;
extern u8 LongEnterPressed;
extern u8 NeedReset;
extern u8 CAN_Time_Out;
extern u16 TechPwd_KWP;
extern u16 ServicePwd_KWP;
extern s8  LCD_Bklight;
extern int LCD_AdjustbarExitCounter;
extern u8 CRC8_POLY;
extern u8 crc8_init;
/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
void Delay (u16 time);
s16 Increment_InRange(s16 var, s16 min_range, s16 max_range);
s16 Decrement_InRange(s16 var, s16 min_range, s16 max_range);
u16 Crc16(u8 *str, u32 length);
extern s16 lookup_truck_fault_object_id(const Truck_fault_msg_st *table, s16 length, s32 target);
extern s16 lookup_CAN_object_id(CAN_param_st *table, s16 length, s32 target);
extern s16 lookup_e_fault_msg_object_id(const Controller_fault_msg_st *table, s16 length, s32 target);
extern s16 lookup_object_id(kwp_data_st *table, s16 length, s32 target);
extern s16 lookup_const_object_id(const kwp_data_st *table, s16 length, s32 target);
extern s16 lookup_fmi_object_id(const J1939_fault_msg_st *table, s16 length, s32 target);
extern s16 lookup_gateway_object_id(const kwp_gateway_data_st *table, s16 length, s32 target);
extern s32 lookup_unichar_object_id(const UniCharIcon *table, s16 length, s32 target);
U8 crc8_compute_bit(MemCanObjData* p_buffer, size_t len, U8 crc8);
#endif
/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/

